import { createRouter, createWebHistory } from 'vue-router';
import store from '@/store';
import HomeView from '@/components/views/HomeView.vue';
import LoginView from '@/components/views/LoginView.vue';
import RegisterView from '@/components/views/RegisterView.vue';
import ProfileView from '@/components/views/ProfileView.vue';
import AddressView from "@/components/views/AddressView.vue";
import CartView from "@/components/views/CartView.vue";
import OrdersView from "@/components/views/OrdersView.vue";
import ProductDetail from "@/components/views/ProductDetail.vue";
import ProductList from "@/components/views/ProductList.vue";
import SearchView from "@/components/views/SearchView.vue";
import NotFound from "@/components/views/NotFound.vue";
// 后台管理页面
import AdminLayout from "@/components/views/admin/AdminLayout.vue";
import UserManagement from "@/components/views/admin/UserManagement.vue";
import AdminManagement from "@/components/views/admin/AdminManagement.vue";
import ProductManagement from "@/components/views/admin/ProductManagement.vue";
import OrderManagement from "@/components/views/admin/OrderManagement.vue";
import CategoryManagement from "@/components/views/admin/CategoryManagement.vue";
import CheckoutView from "@/components/views/CheckoutView.vue";
import PaymentView from "@/components/views/PaymentView.vue";

const routes = [
    { path: '/', redirect: '/home' },
    { path: '/home', name: 'Home', component: HomeView },
    { path: '/login', name: 'Login', component: LoginView },
    { path: '/register', name: 'Register', component: RegisterView },
    { path: '/profile', name: 'Profile', component: ProfileView, meta: { requiresAuth: true } },
    { path: '/address', name: 'Address', component: AddressView, meta: { requiresAuth: true } },
    { path: '/cart', name: 'Cart', component: CartView, meta: { requiresAuth: true } },
    { path: '/checkout', name: 'Checkout', component: CheckoutView, meta: { requiresAuth: true } },
    { path: '/payment/:orderId', name: 'Payment', component: PaymentView, meta: { requiresAuth: true } },
    { path: '/orders', name: 'Orders', component: OrdersView, meta: { requiresAuth: true } },
    { path: '/products', name: 'ProductList', component: ProductList },
    { path: '/product/:id', name: 'ProductDetail', component: ProductDetail },
    { path: '/search', name: 'Search', component: SearchView },
    // 后台管理路由
    {
        path: '/admin',
        name: 'AdminLayout',
        component: AdminLayout,
        meta: { requiresAuth: true, requiresAdmin: true },
        children: [
            { path: '', redirect: '/admin/users' },
            { path: 'users', name: 'UserManagement', component: UserManagement },
            { path: 'admins', name: 'AdminManagement', component: AdminManagement },
            { path: 'products', name: 'ProductManagement', component: ProductManagement },
            { path: 'orders', name: 'OrderManagement', component: OrderManagement },
            { path: 'categories', name: 'CategoryManagement', component: CategoryManagement }
        ]
    },
    { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound }
];

const router = createRouter({
    history: createWebHistory(),
    routes
});

router.beforeEach((to, from, next) => {
    const isAuthenticated = store.getters.isAuthenticated;
    const user = store.getters.user;
    if (to.matched.some(record => record.meta.requiresAuth)) {
        if (!isAuthenticated) {
            next('/login');
        } else if (to.matched.some(record => record.meta.requiresAdmin)) {
            // 检查是否为管理员
            if (user && user.isAdmin === 1) {
                next();
            } else {
                next('/home');
            }
        } else {
            next();
        }
    } else {
        next();
    }
});

export default router;